package com.example.lcpharmacy.service;

import com.example.lcpharmacy.entity.GoodsCategoryEntity;
import com.example.lcpharmacy.entity.GoodsInfoEntity;
import com.example.lcpharmacy.mapper.GoodsCategoryDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsCategoryService {

    @Autowired
    GoodsCategoryDao goodsCategoryDao;

    public PageInfo<GoodsCategoryEntity> goodsCategoryPage(int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsCategoryEntity> goodsCategoryEntities = goodsCategoryDao.selectCategory();
        return new PageInfo<GoodsCategoryEntity>(goodsCategoryEntities);
    }

    /**
     * 更新类目信息
     * @param goodsCategoryEntity
     * @return
     */
    public String updateCategory(GoodsCategoryEntity goodsCategoryEntity){
        List<GoodsCategoryEntity> checkItems1 = goodsCategoryDao.checkUpdateCode(goodsCategoryEntity);
        List<GoodsCategoryEntity> checkItems2 = goodsCategoryDao.checkUpdateName(goodsCategoryEntity);
        if(!(checkItems1.isEmpty())){
            return "类目编号已存在！";
        }else if(!(checkItems2.isEmpty())){
            return "类名名称已存在！";
        }else {
            goodsCategoryDao.updateCodeById(goodsCategoryEntity);
            return "类目信息更新成功！";
        }
    }

    /**
     * 新增类目信息
     * @param goodsCategoryEntity
     * @return
     */
    public String insertCategory(GoodsCategoryEntity goodsCategoryEntity){
        List<GoodsCategoryEntity> checkItems1 = goodsCategoryDao.checkUpdateCode(goodsCategoryEntity);
        List<GoodsCategoryEntity> checkItems2 = goodsCategoryDao.checkUpdateName(goodsCategoryEntity);
        if(!(checkItems1.isEmpty())){
            return "类目编号已存在！";
        }else if(!(checkItems2.isEmpty())){
            return "类名名称已存在！";
        }else {
            goodsCategoryDao.insertCategory(goodsCategoryEntity);
            return "类目信息新增成功！";
        }
    }
}
